require 'test/unit'
require 'database/game_database'

require 'models/entity'
require 'models/container'

class Room < Entity
    include HasManyCharacters
end
class Portal < Entity
    include BelongsToRoom
end
class Character < Entity
    uses_templates_from CharacterDatabase
end
class TCTemplate < Entity
    def create_instance
        inst = Character.new
        inst.name = 'TChar'
        inst.template = self
        inst
    end
end

class GameDatabaseTest < Test::Unit::TestCase
    def setup
        tmp = TCTemplate.new
        tmp.name = "Template"; tmp.oid = 1
        CharacterDatabase.instance.use_templates([tmp],false)
    end
    def test_has_room_response
        p = Portal.new
        assert_respond_to(p,:room)
        assert_respond_to(p,:room_id)
    end
    def test_has_room
        room = Room.new
        room.name = "A Room"
        RoomDatabase.instance.add(room)

        portal = Portal.new
        portal.oid = 99
        portal.name = "door"
        portal.room = room

        assert_equal(room.oid,portal.instance_variable_get(:@props)[:room_id])
        assert_equal(room,portal.room)
    end
    def test_can_hold_characters
        room = Room.new
        room.name = "Room"
        room.oid = 1

        assert_not_equal(0,CharacterDatabase.instance.templates.size)
        tmpl = CharacterDatabase.instance.find_template_by_name("Template")
        assert_not_nil(tmpl)
        char = CharacterDatabase.instance.generate_from_template(tmpl)

        assert(room.add_character(char))
        assert_equal(1,room.characters.size)

        char = CharacterDatabase.instance.generate_from_template(tmpl)
        assert(room.add_character(char))
        assert_equal(2,room.characters.size)

        assert(room.del_character(char))
        assert_equal(1,room.characters.size)
    end

    def test_can_hold_auto
        room = Room.new
        room.name = "Room"
        RoomDatabase.instance.add(room)

        room2 = Room.new
        room2.name = "Room"
        RoomDatabase.instance.add(room2)

        char = Character.new
        char.name = "Character"
        CharacterDatabase.instance.add_instance(char)

        char.room = room
        assert_equal(1,room.characters.size)
        assert_equal(0,room2.characters.size)

        char.room = room2
        assert_equal(0,room.characters.size)
        assert_equal(1,room2.characters.size)
    end

end
